package demo2_example;

interface Usb{
    void openDevice();
    void closeDevice();
}
class Mouse implements Usb{

    @Override
    public void openDevice() {
        System.out.println("打开鼠标");
    }

    @Override
    public void closeDevice() {
        System.out.println("关闭鼠标");
    }
    public void cilck(){
        System.out.println("点击鼠标");
    }
}
class KeyBoard implements Usb{

    @Override
    public void openDevice() {
        System.out.println("打开键盘");
    }

    @Override
    public void closeDevice() {
        System.out.println("关闭键盘");
    }
    public void input(){
        System.out.println("通过键盘输入");
    }
}
class Computer implements Usb{

    @Override
    public void openDevice() {
        System.out.println("开机");
    }

    @Override
    public void closeDevice() {
        System.out.println("关机");
    }
    public void useUSB(Usb usb){
        usb.openDevice();
        if(usb instanceof Mouse){   //此处要使用子类的特有方法，需要将父类向下转型为对应的子类
            Mouse mouse = (Mouse)usb;
            mouse.cilck();
        }
        if(usb instanceof KeyBoard){
            KeyBoard keyboard =  (KeyBoard)usb;
            keyboard.input();
        }
        usb.closeDevice();      //使用动态绑定和多态,调用的对象不同，实现的方法也不同
    }
}
public class Test {
    public static void main(String[] args) {
        Computer com =  new Computer();
        com.openDevice();
        com.useUSB(new Mouse());
        com.useUSB(new KeyBoard());
        com.closeDevice();
    }


}
